// pages/bind_device/bind_device.js
const app = getApp();
const { bind } = require('../../config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    codes:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    let { id, type, isSigned, status,blackInit, coloursInit, location, codes } = options;
    this.setData({
      id,
      type,
      isSigned,
      status,
      isSigned,
      blackInit,
      coloursInit,
      codes: codes || '',
      location: location || ''
    })
    // console.log(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  scanCode: function (){
    let that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        if (res.scanType != 'CODE_128'){
          wx.showToast({
            title: '请扫描盒子上的条形码',
            icon: 'none',
            duration: 2000
          })
          return
        }else{
          if (res.result.substring(0, 6) != '100101' && res.result.substring(0, 7) != 'T100101') {
            wx.showToast({
              title: '请扫描盒子上的条形码',
              icon: 'none',
              duration: 2000
            })
            return
          }
          that.setData({
            codes: res.result,
          })
        }  
  
      }
    })
  },
  formSubmit: function (e) {
    let { blackCount, coloursCount, location, codes  } = e.detail.value;
    console.log(e.detail.value)
    let { id, type, isSigned } = this.data;
    let data ;
    if(isSigned == 1){
      if(type == 1){
        if (!blackCount || !coloursCount || !location || !codes) {
          wx.showToast({
            title: '请填写完整信息',
            icon: 'none'
          })
          return
        }
        data = { blackCount, coloursCount, location, codes, id }
      }
      if (type == 2){
        if (!location || !codes) {
          wx.showToast({
            title: '请填写完整信息',
            icon: 'none'
          })
          return
        }
        data = { blackCount, coloursCount,location, codes, id }
      }
    } else {
      if (!codes) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none'
        })
        return
      }
      data = {  codes, id }
    }
    app.appRequest({
      url: bind,
      method: 'POST',
      data:{
        ...data
      },
      success: (res)=>{
        if(res.code==0){
          wx.navigateBack({
            delta:1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    })
  }
})